﻿
$(function() {
    $(".page-topbar-body").hide();
    $(".page-head").hide();
    $(".page-menu").hide();
    hideOrgTree();
    hideStaffList();
});

$('.textbox input').each(function(index, ele) {
    var node = $(ele);
    if (node.val() == '') {
        node.val(node.attr('tip'));
        node.parent().addClass('textbox-tip');
    }
});

//当有焦点显示下拉框，组织显示树，员工显示列表
$('.textbox input').focus(function(e) {
    var el = $(e.currentTarget);
    el.parent().addClass('textbox-focus');
    el.parent().removeClass('textbox-error');
    el.parent().removeClass('textbox-tip');
    if (el.val() == el.attr('tip')) {
        el.val('');
    }
    switch (el.attr("id")) {
        case "org_name":
            showOrgTree();
            break;
        case "staff_name":
            var input = $("#org_name");
            //如果没有选择组织，不显示员工列表
            if (input.val() != input.attr('tip'))
                showStaffList();
            break;
        default:
            break;
    }
});

$('.textbox input').blur(function(e) {
    var el = $(e.currentTarget);
    el.parent().removeClass('textbox-focus');
    if (!el.val() || el.val() == el.attr('tip')) {
        el.val(el.attr('tip'));
        el.parent().addClass('textbox-tip');
    }
});

//显示组织树

function showOrgTree() {
    var treeJson = getDemoOrgJson();

    var setting = {
        view: {
            dblClickExpand: false,
            showLine: true
        },
        callback: {
            onClick: onClick
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    //树的点击事件

    function onClick(e, treeId, treeNode) {
        if (treeNode.type != "organization") { //如果不是网点节点，将单击展开节点
            var zTree = $.fn.zTree.getZTreeObj(treeId);
            zTree.expandNode(treeNode);
        }
        else { //是网点节点，将网点数据提取放入TextBox中
            var input = $("#org_name");
            input.val(treeNode.id + ":" + treeNode.name);
            input.parent().removeClass('textbox-tip');
            hideOrgTree();
        }
    }

    //根据数据生成树
    $.fn.zTree.init($("#organization-tree"), setting, treeJson);

    var panel = buildDropPanel("#org_name", "#orgtree-panel");
    panel.show(100);
}

function hideOrgTree() {
    var panel = $("#orgtree-panel");
    panel.hide();
}

function showStaffList() {
    var listJson = getStaffListDemoJson();

    var tableOld = $(".stafflist-table");
    if (tableOld != null)
        tableOld.remove();

    var table = "<table class='stafflist-table'>";
    for (var i = 0; i < listJson.length; i++) {
        table = table + "<tr><td class='staffid-td'>";
        var item = listJson[i];
        table = table + item.id + "</td><td class='staffname-td'>" + item.name + "</td></tr>";
    }
    table = table + "</table>";
    $("#stafflist-panel").append(table);

    var panel = buildDropPanel("#staff_name", "#stafflist-panel");
    panel.show(200);

    $("#stafflist-panel tr").click(function() {
        var td1 = $(this).children("td:eq(0)", "#stafflist-panel").html();
        var td2 = $(this).children("td:eq(1)", "#stafflist-panel").html();
        var input = $("#staff_name");
        input.val(td1 + ":" + td2);
        input.parent().removeClass('textbox-tip');
        hideStaffList();
    });
}

function hideStaffList() {
    var panel = $("#stafflist-panel");
    panel.hide();
}

function buildDropPanel(boxNodeId, panelId) {
    var node = $(boxNodeId);
    var offset = node.offset();

    var top = offset.top;
    var left = offset.left;
    var width = node.width();
    var height = node.height();

    var panel = $(panelId);
    var css = {
        'border': '1px solid #CCCCCC',
        'background-color': '#FFF',
        'width': width + 20 + 'px',
        'height': 220 + 'px',
        'position': 'fixed',
        'left': left + 'px',
        'top': top + height + 20 + 'px',
        'opacity': '0.9',
        'z-index': 1,
        'overflow': 'auto'
    };
    panel.css(css);
    return panel;
}

//登录
$("#btn_login").live("click", function() {
    var org = $("#org_name");
    var staff = $("#staff_name");
    var pwd = $("#login-password");

    var postJson = "{'Organization':'" + org.val() + "','Staff':'" + staff.val() + "','Password':'" + pwd.val() + "'}";
    $.ajax({
        url: "/UserLogin",
        cache: false,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: postJson,
        statusCode: {
            200: function(result) {
                $.cookies.set('OrganizationId', "'"+result.OrganizationId+"'");
                $.cookies.set('OrganizationName', "'"+result.OrganizationName+"'");
                $.cookies.set('StaffId', "'"+result.Id+"'");
                $.cookies.set('StaffName', "'"+result.Name+"'");
                $.cookies.set('StaffRoleId', "'" + result.RoleId + "'");

                var p = "<p>" + result.OrganizationId + "<i></i>"
                    + result.OrganizationName + "<i></i>"
                    + result.Id + "<i></i>"
                    + result.Name + "<i></i>"
                    + result.RoleId + "<i></i>"
                    + result.LoginStatus + "<i></i>"
                    + result.LastLoginTime + "<i></i></p>";
                showDebugInfo(p);
                
                if (result.LoginStatus == 1)//登录成功
                    location.href = '../../';
            }
        },
    });
});

//登录时触发相关事件

function setLoginCookie() {
    if ($("#rmb_username").attr("checked") == true) {
        $.cookie('login_cookie_username', $.trim($("#user").val()), { expires: 30 });
    }
    else {
        $.cookie('login_cookie_username', null);
    }

    if ($("#rmb_pwd").attr("checked") == true) {
        $.cookie('login_cookie_password', $.trim($("#password").val()), { expires: 30 });
    }
    else {
        $.cookie('login_cookie_password', null);
    }

    if ($("#rmb_company").attr("checked") == true) {
        $.cookie('login_cookie_companyid', $.trim($("#corporation").val()), { expires: 30 });
    }
    else {
        $.cookie('login_cookie_companyid', null);
    }

}

//加载cookie记住的信息(显示)
function loadCookie() {
    var cookievalue;
    if ($.cookie("login_cookie_username") != null && $.cookie("login_cookie_username") != "") {
        cookievalue = $.cookie("login_cookie_username");
        $("#user").val(cookievalue);
        $("#rmb_username").attr("checked", true);
    }

    if ($.cookie("login_cookie_password") != null && $.cookie("login_cookie_password") != "") {
        cookievalue = $.cookie("login_cookie_password");
        $("#password").val(cookievalue);
        $("#rmb_pwd").attr("checked", true);
    }

    if ($.cookie("login_cookie_companyid") != null && $.cookie("login_cookie_companyid") != "") {
        cookievalue = $.cookie("login_cookie_companyid");
        $("#corporation").val(cookievalue);
        $("#rmb_company").attr("checked", true);
    }

}


//---------测试数据-------------------

//返回一个组织的基本树测试数据

function getDemoOrgJson() {
    var json = [
        { id: 1, pId: 0, name: "上海市商业银行", type: "root", open: true },
        { id: 11, pId: 1, name: "宝山", type: "category" },
        { id: 9001, pId: 11, name: "上海车站南路分理处", type: "organization" },
        { id: 9002, pId: 11, name: "上海市安龙路支行", type: "organization" },
        { id: 9003, pId: 11, name: "上海市安顺路支行", type: "organization" },
        { id: 9004, pId: 11, name: "上海市安亭支行", type: "organization" },
        { id: 9005, pId: 11, name: "上海市鞍山路支行", type: "organization" },
        { id: 9006, pId: 11, name: "上海市八埭头支行", type: "organization" },
        { id: 9007, pId: 11, name: "上海市八一路支行", type: "organization" },
        { id: 9008, pId: 11, name: "上海市白杨路支行", type: "organization" },
        { id: 9009, pId: 11, name: "上海市宝钢国贸支行", type: "organization" },
        { id: 9010, pId: 11, name: "上海市长兴支行", type: "organization" },
        { id: 12, pId: 1, name: "崇明", type: "category" },
        { id: 111, pId: 12, name: "上海市长阳支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市常熟路支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城东支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城市工业园区支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城中路支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市程家桥支行", type: "organization" },
        { id: 13, pId: 1, name: "嘉定", type: "category" },
        { id: 111, pId: 13, name: "上海市大华支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大连路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大连支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大木桥路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大宁支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大同路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大统支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市德州路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市滴水湖支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市地铁人民广场站支行", type: "organization" },
        { id: 14, pId: 1, name: "青浦", type: "category" },
        { id: 111, pId: 14, name: "上海市崇明支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市崇明支行新河分理处", type: "organization" },
        { id: 111, pId: 14, name: "上海市川北支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市川沙支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市春申支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市打浦桥支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市大场支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市大渡河路支行", type: "organization" },
        { id: 15, pId: 1, name: "松江", type: "category" },
        { id: 111, pId: 15, name: "上海市第二营业部", type: "organization" },
        { id: 111, pId: 15, name: "上海市第一支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市滇池路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市电气支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市丁香花园支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市丁香路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市定海路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市定西路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市东安路支行", type: "organization" },
        { id: 16, pId: 1, name: "金山", type: "category" },
        { id: 116, pId: 16, name: "上海市宝钢支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行场中分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行共富分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行临江分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市堡镇中路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市报春路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北蔡支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北京西路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北渔路支行", type: "organization" },
        { id: 17, pId: 1, name: "奉贤", type: "category" },
        { id: 117, pId: 17, name: "上海市北站支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市碧江路支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市碧云国际支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市博山路支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市不夜城支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市财富广场支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹家渡支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹杨新村第二支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹杨新村支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市漕宝路支行", type: "organization" },
        { id: 18, pId: 1, name: "闵行", type: "category" },
        { id: 118, pId: 18, name: "上海市漕河泾开发区支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市昌化路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市昌里东路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长白支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长岛路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长江支行", type: "organization" },
        { id: 19, pId: 1, name: "浦东", type: "category" },
        { id: 119, pId: 19, name: "上海市长宁支行曹家渡分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行长宁路第二分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行空港分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行娄山关路第二分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长桥支行", type: "organization" },
        { id: 119, pId: 19, name: "上海市长寿路支行", type: "organization" }
    ];
    return json;
}

function getStaffListDemoJson() {
    var json = [
        { id: 1001, name: '王屹欧' },
        { id: 1002, name: '袁靖航' },
        { id: 1003, name: '哈帆' },
        { id: 1004, name: '杨天' },
        { id: 1005, name: '何沁航' },
        { id: 1006, name: '欧阳小雪' },
        { id: 1007, name: '宁偌娴' },
        { id: 1008, name: '赵文堃' },
        { id: 1009, name: '陈曦' },
        { id: 1010, name: '吴晗' },
        { id: 1011, name: '王雨帆' },
        { id: 1012, name: '张岩' },
        { id: 1013, name: '张锐琦' },
        { id: 1014, name: '赵熙' },
        { id: 1015, name: '乔子繁' },
        { id: 1016, name: '窦帅' },
        { id: 1017, name: '刘嘉明' },
        { id: 1018, name: '李奥奇' },
        { id: 1019, name: '史昭彪' },
        { id: 1020, name: '王傲' },
        { id: 1021, name: '邓学斌' },
        { id: 1022, name: '李林峰' },
        { id: 1023, name: '郭梦麒' },
        { id: 1024, name: '车天宇' },
        { id: 1025, name: '付琳' },
        { id: 1026, name: '严梦晨' },
        { id: 1027, name: '雷峻' },
        { id: 1028, name: '师鹏程' },
        { id: 1029, name: '马家明' },
        { id: 1030, name: '马家亮' },
        { id: 1031, name: '雷少华' },
        { id: 1032, name: '石佳琪' }
    ];
    return json;
}